/*
 * Está classe será utilizada para conectar ao banco de dados
 */

package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import DAO.DAOExceptionMelhorCinema;

/**
*
* @author Natalia
*/

public class ConnectionFactory {
	
	//Inicia uma conexão
	 public static Connection getConnection() throws DAOExceptionMelhorCinema{
	       try{
	           Class.forName("com.mysql.jdbc.Driver");
	           return DriverManager.getConnection("jdbc:mysql://localhost/melhorcinemadb", "root","root");
	       }catch(Exception e){
	          System.out.println(e.getLocalizedMessage());
	          return null;
	       }
	   }
	 
	 public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) throws DAOExceptionMelhorCinema {
	        close(conn, stmt, rs);
	    }

	    public static void closeConnection(Connection conn, Statement stmt) throws DAOExceptionMelhorCinema {
	        close(conn, stmt, null);
	    }

	    public static void closeConnection(Connection conn) throws DAOExceptionMelhorCinema {
	        close(conn, null, null);
	    }
	 
	 //Fecha a conexão	
	   public static void close(Connection conn, Statement stmt, ResultSet rs) throws DAOExceptionMelhorCinema{
	       try{
	           if (rs != null) rs.close();
	           if (stmt != null) stmt.close();
	           if (conn != null) conn.close();
	       }catch(Exception e){
	    	   System.out.println(e.getLocalizedMessage());
	       }
	   }


}
